﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Services;
using System.Collections;
using Catering.View_Operaction;
using Catering_DBOpertion;

namespace Catering.yl.Daily
{
    public partial class DailyDdynamic : System.Web.UI.Page
    {
        public string strClassID = "";
        public string struseDate = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ClassID"] != null)
                strClassID = Session["ClassID"].ToString();
            if (Session["useDate"] != null)
                struseDate = Session["useDate"].ToString();
            if( !Page.IsPostBack )
            {
                txt_gongzuo.Text = "";
                txt_zhiliang.Text = "";
                LoadForm();
            }            
        }
        public void LoadForm()
        {
            try
            {
                DataTable dt = new Catering_Rules.DailyDynamic().GetList("Date='" + Convert.ToDateTime(struseDate).ToString() + "' and ClassID=" + strClassID).Tables[0];
                if( dt.Rows.Count > 0 )
                {
                    txt_gongzuo.Text = dt.Rows[0]["Service"].ToString();                    
                    txt_zhiliang.Text = dt.Rows[0]["Quality"].ToString();
                }                
            }
            catch
            {

            }
        }
        protected void Save_Click(object sender, EventArgs e)
        {
            if(txt_zhiliang.Text=="")
            {
                string strError = "alert('请填写完整的正确信息！')";
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strError, true);
            }
            if(txt_gongzuo.Text=="")
            {
                string strError = "alert('请填写完整的正确信息！')";
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strError, true);
            }
            if (IsExists(struseDate, strClassID))
            {
                new Catering_Rules.DailyDynamic().Delete("Date='" + Convert.ToDateTime(struseDate).ToString() + "' and ClassID=" + strClassID);
            }
            try
            {
                Catering_Model.DailyDynamic dm = new Catering_Model.DailyDynamic();
                dm.ClassID = int.Parse(strClassID);
                dm.Date = Convert.ToDateTime(struseDate);
                dm.Service = txt_gongzuo.Text;
                dm.Quality = txt_zhiliang.Text;
                new Catering_Rules.DailyDynamic().Add(dm);
                string strOpenUrl = "alert('保存成功');window.location.href = window.location.href;";
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strOpenUrl, true);
            }
            catch
            {
                string strOpenUrl = "alert('保存失败');window.location.href = window.location.href;";
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strOpenUrl, true);
            }           
        }
        public bool IsExists(string strdate, string ClassID)
        {
            DataTable dt = new Catering_Rules.DailyDynamic().GetList("Date='" + Convert.ToDateTime(strdate).ToString() + "' and ClassID=" + ClassID).Tables[0];            
            if (dt.Rows.Count == 0)
                return false;
            return true;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DataTable dt_Plan_old = new Catering_Rules.AttendanceRecord().GetAllList("Date between '2014/09/01' and '2014/10/30'").Tables[0];
            DataTable dt_Plan = dt_Plan_old.Copy();
            foreach (DataRow dr in dt_Plan_old.Rows)
            {
                if (dt_Plan.Select("PName='" + dr["PName"].ToString() + "' and Date='" + dr["Date"].ToString() + "' and ClassID='" + dr["ClassID"].ToString() + "'").Count() > 1)
                {
                    dt_Plan.AcceptChanges();
                    foreach (DataRow dr1 in dt_Plan.Rows)
                    {
                        if (dr1["PName"].ToString() == dr["PName"].ToString() && dr1["Date"].ToString() == dr["Date"].ToString() && dr1["ClassID"].ToString() == dr["ClassID"].ToString())
                            dr1.Delete();
                    }
                    new Catering_Rules.AttendanceRecord().DeleteListBywhere("Date='" + Convert.ToDateTime(dr["Date"].ToString()).ToString() + "' and ClassID=" + dr["ClassID"].ToString() + " and PName ='" + dr["PName"].ToString() + "'");
                    Catering_Model.AttendanceRecord md = new Catering_Model.AttendanceRecord();
                    md.Date = Convert.ToDateTime(dr["Date"].ToString());
                    md.ClassID = int.Parse(dr["ClassID"].ToString());
                    md.PID = int.Parse(dr["PID"].ToString());
                    md.PName = dr["PName"].ToString();
                    md.State = dr["State"].ToString();
                    md.temp1 = dr["temp1"].ToString();
                    md.temp2 = dr["temp2"].ToString();
                    new Catering_Rules.AttendanceRecord().Add(md);
                }
            }
            //for (int i = 0; i < dt_Plan.Rows.Count;i++ )
            //{
            //    DataTable dt_Plan_new = new Catering_Rules.AttendanceRecord().GetAllList("ClassID=" + dt_Plan.Rows[i]["ClassID"].ToString() + " and Date='" + Convert.ToDateTime(dt_Plan.Rows[i]["Date"].ToString()) + "'").Tables[0];// and PName='"+dt_Plan.Rows[i]["PName"].ToString()+"'
            //        DataTable dt_Plan_left = dt_Plan_new.Clone();
            //    if(dt_Plan_new.Rows.Count>0)
            //    {
            //        foreach (DataRow dr in dt_Plan_new.Rows)
            //        {
            //            DataRow dr_Plan = dt_Plan_left.NewRow();

            //            bool bFlag = true;

            //            for (int k = 0; k < dt_Plan_left.Rows.Count; k++)
            //            {

            //                if (dr["ClassID"].ToString() == dt_Plan.Rows[k]["ClassID"].ToString() &&
            //                            dr["Date"].ToString() == dt_Plan.Rows[k]["Date"].ToString() &&
            //                            dr["PName"].ToString() == dt_Plan.Rows[k]["PName"].ToString() &&
            //                            dr["State"].ToString() == dt_Plan.Rows[k]["State"].ToString())
            //                    bFlag = false;

            //            }

            //            if (bFlag)
            //            {
            //                foreach (DataColumn dc in dt_Plan_new.Columns)
            //                {
            //                    dr_Plan[dc.ColumnName] = dr[dc.ColumnName];
            //                }
            //                dt_Plan_left.Rows.Add(dr_Plan);
            //            }
            //        }
            //    }

            //    for (int m = 0; m < dt_Plan_left.Rows.Count; m++)
            //    {
            //        Catering_Model.AttendanceRecord md = new Catering_Model.AttendanceRecord();
            //        DateTime strDate;
            //        if (DateTime.TryParse(dt_Plan_left.Rows[m]["Date"].ToString(), out strDate))
            //            md.Date = strDate;
            //        else
            //            continue;
            //        md.ClassID = int.Parse(dt_Plan_left.Rows[m]["ClassID"].ToString());
            //        md.PID = int.Parse(dt_Plan_left.Rows[m]["PID"].ToString());
            //        md.PName = dt_Plan_left.Rows[m]["PName"].ToString();
            //        md.State = dt_Plan_left.Rows[m]["State"].ToString();
            //        md.temp1 = dt_Plan_left.Rows[m]["temp1"].ToString();
            //        md.temp2 = dt_Plan_left.Rows[m]["temp2"].ToString();
            //        new Catering_Rules.AttendanceRecord().Add(md);
            //    }
            //    new Catering_Rules.AttendanceRecord().DeleteListBywhere("Date='" + Convert.ToDateTime(dt_Plan.Rows[i]["Date"].ToString()).ToString() + "' and ClassID=" + dt_Plan.Rows[i]["ClassID"].ToString());
            //}
            string strOpenUrl = "alert('保存成功');window.location.href = window.location.href;";
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strOpenUrl, true);
        }
    }
}